# -*- coding: utf-8 -*-
#
#  Copyright (C) 2009 GSyC/LibreSoft
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
#    Author : Jose Antonio Santos Cadenas <jcaden __at__ gsyc __dot__ es>
#

from format.utils import  getResponseFormat, generateResponse
from social.core import api

from utils import error

def privacy(request, node_id=None):
    format = getResponseFormat (request)
    if not request.user.is_authenticated():
        return error(format, "The user is not authenticated")
    if node_id == None:
        node_id = request.user.id
    success, perm = api.privacy.get_permissions(request.user.id, node_id)
    if success:
        data = {'code'         : '200',
                'privacy' : perm}
        return generateResponse(format, data, "privacy/privacy")
    else:
        return error(format, perm)

def change_privacy(request, node_id=None):
    
    format = getResponseFormat (request)
    if not request.user.is_authenticated():
        return error(format, "The user is not authenticated")
    if request.method != "POST":
        return error(format, "Need a POST petition")
    if node_id == None:
        node_id = request.user.id
    if "perm" in request.POST:
        perm = request.POST["perm"]
    else:
        perm = None
    if "field" in request.POST:
        field = request.POST["field"]
    else:
        field = None
        
    success, status = api.privacy.privacy_change(user_id=request.user.id, node_id=node_id, 
                                                 perm=perm, field = field)
    if success:
        data = {'code'             : '200',
                'description'      : "Permissions updated correctly"}
        return generateResponse(format, data, "ok")
    else:
        return error(format, status)

#def allow(request, undo=False):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "Need a POST petition")
    ##Else new roles allowed
    #if "field" in request.POST:
        #field=request.POST["field"]
        #if field=="":
            #field=None
    #else:
        #field=None
    #if "role" in request.POST:
        #role=request.POST["role"]
        #if role=="":
            #role=[]
        #else:
            #role=[role]
    #else:
        #role=[]
    #if "user" in request.POST:
        #user=request.POST["user"]
        #if user=="":
            #user=[]
        #else:
            #user=[user]
    #else:
        #user=[]
    #success, status = api.privacy.allow_access(user_id=request.user.id, field=field,
                                               #roles=role, users=user, undo=undo)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)
    

#def restrict(request, undo=False):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "Need a POST petition")
    ##Else new roles restricted
    #if "field" in request.POST:
        #field=request.POST["field"]
        #if field=="":
            #field=None
    #else:
        #field=None
    #if "role" in request.POST:
        #role=request.POST["role"]
        #if role=="":
            #role=[]
        #else:
            #role=[role]
    #else:
        #role=[]
    #if "user" in request.POST:
        #user=request.POST["user"]
        #if user=="":
            #user=[]
        #else:
            #user=[user]
    #else:
        #user=[]
    #success, status = api.privacy.restrict_access(user_id=request.user.id, field=field,
                                                  #roles=role, users=user, undo=undo)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)    

#def note_privacy(request, note_id):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #success, perm = api.privacy.get_note_permissions(note_id, request.user.id)
    #if success:
        #data = {'code'         : '200',
                #'note_privacy' : perm}
        #return generateResponse(format, data, "privacy/note")
    #else:
        #return error(format, perm)

#def note_allow(request, note_id=None, undo=False):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "Need a POST petition")
    #if "field" in request.POST:
        #field=request.POST["field"]
        #if field=="":
            #field=None
    #else:
        #field=None
    #if "role" in request.POST:
        #role=request.POST["role"]
        #if role=="":
            #role=[]
        #else:
            #role=[role]
    #else:
        #role=[]
    #if "user" in request.POST:
        #user=request.POST["user"]
        #if user=="":
            #user=[]
        #else:
            #user=[user]
    #else:
        #user=[]
    #success, status = api.privacy.note_allow_access(user_id=request.user.id, note_id=note_id,
                                                    #roles=role, users=user, undo=undo, field=field)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)
    

#def note_restrict(request, note_id=None, undo=False):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "Need a POST petition")
    ##Else new roles restricted
    #if "field" in request.POST:
        #field=request.POST["field"]
        #if field=="":
            #field=None
    #else:
        #field=None
    #if "role" in request.POST:
        #role=request.POST["role"]
        #if role=="":
            #role=[]
        #else:
            #role=[role]
    #else:
        #role=[]
    #if "user" in request.POST:
        #user=request.POST["user"]
        #if user=="":
            #user=[]
        #else:
            #user=[user]
    #else:
        #user=[]
    #success, status = api.privacy.note_restrict_access(user_id=request.user.id, note_id=note_id,
                                                       #roles=role, users=user, undo=undo, field=field)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)  
    
#def photo_privacy(request, photo_id):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #success, perm = api.privacy.get_photo_permissions(photo_id, request.user.id)
    #if success:
        #data = {'code'         : '200',
                #'photo_privacy' : perm}
        #return generateResponse(format, data, "privacy/photo")
    #else:
        #return error(format, perm)

#def photo_allow(request, photo_id=None, undo=False):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "Need a POST petition")
    #if "field" in request.POST:
        #field=request.POST["field"]
        #if field=="":
            #field=None
    #else:
        #field=None
    #if "role" in request.POST:
        #role=request.POST["role"]
        #if role=="":
            #role=[]
        #else:
            #role=[role]
    #else:
        #role=[]
    #if "user" in request.POST:
        #user=request.POST["user"]
        #if user=="":
            #user=[]
        #else:
            #user=[user]
    #else:
        #user=[]
    #success, status = api.privacy.photo_allow_access(user_id=request.user.id, photo_id=photo_id,
                                               #roles=role, users=user, undo=undo, field=field)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)
    

#def photo_restrict(request, photo_id=None, undo=False):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "Need a POST petition")
    ##Else new roles restricted
    #if "field" in request.POST:
        #field=request.POST["field"]
        #if field=="":
            #field=None
    #else:
        #field=None
    #if "role" in request.POST:
        #role=request.POST["role"]
        #if role=="":
            #role=[]
        #else:
            #role=[role]
    #else:
        #role=[]
    #if "user" in request.POST:
        #user=request.POST["user"]
        #if user=="":
            #user=[]
        #else:
            #user=[user]
    #else:
        #user=[]
    #success, status = api.privacy.photo_restrict_access(user_id=request.user.id, photo_id=photo_id,
                                                   #field=field, roles=role, users=user, undo=undo)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)
    
#def sound_privacy(request, sound_id):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #success, perm = api.privacy.get_sound_permissions(sound_id, request.user.id)
    #if success:
        #data = {'code'         : '200',
                #'sound_privacy' : perm}
        #return generateResponse(format, data, "privacy/sound")
    #else:
        #return error(format, perm)

#def sound_restrict(request, sound_id=None, undo=False):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "Need a POST petition")
    ##Else new roles restricted
    #if "field" in request.POST:
        #field=request.POST["field"]
        #if field=="":
            #field=None
    #else:
        #field=None
    #if "role" in request.POST:
        #role=request.POST["role"]
        #if role=="":
            #role=[]
        #else:
            #role=[role]
    #else:
        #role=[]
    #if "user" in request.POST:
        #user=request.POST["user"]
        #if user=="":
            #user=[]
        #else:
            #user=[user]
    #else:
        #user=[]
    #success, status = api.privacy.sound_restrict_access(user_id=request.user.id, sound_id=sound_id,
                                                        #field=field, roles=role, users=user, undo=undo)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)

#def sound_allow(request, sound_id=None, undo=False):
    #format = getResponseFormat (request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "Need a POST petition")
    #if "field" in request.POST:
        #field=request.POST["field"]
        #if field=="":
            #field=None
    #else:
        #field=None
    #if "role" in request.POST:
        #role=request.POST["role"]
        #if role=="":
            #role=[]
        #else:
            #role=[role]
    #else:
        #role=[]
    #if "user" in request.POST:
        #user=request.POST["user"]
        #if user=="":
            #user=[]
        #else:
            #user=[user]
    #else:
        #user=[]
    #success, status = api.privacy.sound_allow_access(user_id=request.user.id, sound_id=sound_id,
                                               #roles=role, users=user, undo=undo, field=field)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)

def get_roles(request):
    format = getResponseFormat (request)
    if not request.user.is_authenticated():
        return error(format, "The user is not authenticated")
    roles=api.privacy.get_all_roles()
    data = {'code'         : '200',
            'roles'        : roles}
    return generateResponse(format, data, "privacy/roles")

def get_permissions(request):
    format = getResponseFormat (request)
    if not request.user.is_authenticated():
        return error(format, "The user is not authenticated")
    roles=api.privacy.get_permissions_names()
    data = {'code'         : '200',
            'roles'        : roles}
    return generateResponse(format, data, "privacy/roles")

#def node_allow_all(request, node_id, undo=False):
    #format = getResponseFormat(request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "A post petition is needed")
    #user_id = request.user.id
    #if "role" not in request.POST:
        #roles =[]
    #else: 
        #roles = [request.POST["role"]]
    #if "user" not in request.POST:
        #users =[]
    #else: 
        #users = [request.POST["user"]]
    #success, status = api.privacy.node_allow_all(user_id, node_id, roles=roles, users=users, undo=undo)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions updated correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)
    
#def node_reset(request, node_id):
    #format = getResponseFormat(request)
    #if not request.user.is_authenticated():
        #return error(format, "The user is not authenticated")
    #if request.method != "POST":
        #return error(format, "A post petition is needed")
    #user_id = request.user.id
    #success, status = api.privacy.reset_privacy(user_id, node_id)
    #if success:
        #data = {'code'             : '200',
                #'description'      : "Permissions reseted correctly"}
        #return generateResponse(format, data, "ok")
    #else:
        #return error(format, status)
